<template>
    <div class="padding-lr-xs">
        <a-form v-model="asyncInfo">
            <a-form-item>
                <a-checkbox value="asyncBefore" v-model="asyncInfo.asyncBefore" @change="updateAsyncInfo">
                    异步前
                </a-checkbox>
                <a-checkbox value="asyncAfter" v-model="asyncInfo.asyncAfter" @change="updateAsyncInfo">
                    异步后
                </a-checkbox>
                <a-checkbox value="exclusive" v-model="asyncInfo.exclusive" v-if="asyncInfo.asyncAfter || asyncInfo.asyncBefore" @change="updateAsyncInfo">
                    排除
                </a-checkbox>
            </a-form-item>
        </a-form>
        <a-divider>工作配置</a-divider>
        <a-form v-model="jobInfo" v-if="asyncInfo.asyncAfter || asyncInfo.asyncBefore">
            <a-form-item label="工作优先级:">
                <a-input placeholder="请输入工作优先级" v-model="jobInfo.jobPriority" @change="updateJobPriority"/>
            </a-form-item>
            <a-form-item label="重试时间周期:">
                <a-input placeholder="请输入重试时间周期" v-model="jobInfo.timeCycle" @change="updateTimeCycle"/>
            </a-form-item>
        </a-form>
    </div>
</template>

<script>
    import BpmnHelper from '../helper/BpmnHelper';
    export default {
        name:'AsyncSetting',
        props: {
            formData: {
				type: Object,
				require: true,
			},
			modeler: {
                type: Object,
                required: true,
			},
            nodeElement: {
                type: Object,
				required: true,
            }
        },
        watch: {
		    nodeElement: {
                deep: true,
                immediate: true,
                handler(newVal, oldVal) {
                    const businessObject = newVal.businessObject
                    
                }   
            }
        },
        data() {
            return {
                asyncInfo: {
                    asyncAfter: false,
                    asyncBefore: false,
                    exclusive: false
                },
                jobInfo: {
                    jobPriority: '',
                    timeCycle: ''
                },
            }
        },
        methods: {
            // 异步前后
            updateAsyncInfo() {
                if (!this.asyncInfo.asyncBefore && !this.asyncInfo.asyncAfter) {
                    this.asyncInfo.exclusive = false;
                    this.jobInfo.jobPriority = '';
                    this.jobInfo.timeCycle = '';
                    this.updateJobPriority();
                    this.updateTimeCycle();
                }
                this.modeler.get("modeling").updateProperties(this.nodeElement, { ...this.asyncInfo });
            },
            // 工作优先级
            updateJobPriority() {
                const properties = Object.create(null)
                properties.jobPriority = this.jobInfo.jobPriority
                this.modeler.get("modeling").updateProperties(this.nodeElement, { ...properties });
            },
            // 重试周期
            updateTimeCycle() {
                let extensionElements = this.nodeElement.businessObject.get('extensionElements');
                if (!extensionElements) {
                    extensionElements = window.processInstances.bpmnFactory.create('bpmn:ExtensionElements');
                }
                extensionElements.values = extensionElements.values?.filter(item => item.$type !== 'flowable:FailedJobRetryTimeCycle') ?? []
                if (this.jobInfo.timeCycle) {
                    const properties = { body: this.jobInfo.timeCycle }
                    const timeCycleElementName = 'flowable:FailedJobRetryTimeCycle'
                    const timeCycleElement = window.processInstances.bpmnFactory.create(timeCycleElementName, properties)
                    extensionElements.get('values').push(timeCycleElement)
                    this.modeler.get("modeling").updateProperties(this.nodeElement, { extensionElements: extensionElements })
                }
            },
        },
    }
</script>

<style lang="scss" scoped>

</style>